home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / lib / strtod.c < prev    next >
C/C++ Source or Header  |  1992-10-28  |  4KB  |  189 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #include <ctype.h>
  21. #include <math.h>
  22.  
  23. #if STDC_HEADERS
  24. #include <float.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #else
  28. #define NULL 0
  29. #define DBL_MAX 1.7976931348623159e+308
  30. #define DBL_MIN 2.2250738585072010e-308
  31. extern int errno;
  32. #endif
  33. #ifndef HUGE_VAL
  34. #define HUGE_VAL HUGE
  35. #endif
  36.  
  37. #if !__STDC__
  38. #define const
  39. #endif
  40.  
  41. /* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
  42.    character after the last one used in the number is put in *ENDPTR.  */
  43. double
  44. strtod (nptr, endptr)
  45.      const char *nptr;
  46.      char **endptr;
  47. {
  48.   register const char *s;
  49.   short int sign;
  50.  
  51.   /* The number so far.  */
  52.   double num;
  53.  
  54.   int got_dot;            /* Found a decimal point.  */
  55.   int got_digit;        /* Seen any digits.  */
  56.  
  57.   /* The exponent of the number.  */
  58.   long int exponent;
  59.  
  60.   if (nptr == NULL)
  61.     {
  62.       errno = EINVAL;
  63.       goto noconv;
  64.     }
  65.  
  66.   s = nptr;
  67.  
  68.   /* Eat whitespace.  */
  69.   while (isspace (*s))
  70.     ++s;
  71.  
  72.   /* Get the sign.  */
  73.   sign = *s == '-' ? -1 : 1;
  74.   if (*s == '-' || *s == '+')
  75.     ++s;
  76.  
  77.   num = 0.0;
  78.   got_dot = 0;
  79.   got_digit = 0;
  80.   exponent = 0;
  81.   for (;; ++s)
  82.     {
  83.       if (isdigit (*s))
  84.     {
  85.       got_digit = 1;
  86.  
  87.       /* Make sure that multiplication by 10 will not overflow.  */
  88.       if (num > DBL_MAX * 0.1)
  89.         /* The value of the digit doesn't matter, since we have already
  90.            gotten as many digits as can be represented in a `double'.
  91.            This doesn't necessarily mean the result will overflow.
  92.            The exponent may reduce it to within range.
  93.  
  94.            We just need to record that there was another
  95.            digit so that we can multiply by 10 later.  */
  96.         ++exponent;
  97.       else
  98.         num = (num * 10.0) + (*s - '0');
  99.  
  100.       /* Keep track of the number of digits after the decimal point.
  101.          If we just divided by 10 here, we would lose precision.  */
  102.       if (got_dot)
  103.         --exponent;
  104.     }
  105.       else if (!got_dot && *s == '.')
  106.     /* Record that we have found the decimal point.  */
  107.     got_dot = 1;
  108.       else
  109.     /* Any other character terminates the number.  */
  110.     break;
  111.     }
  112.  
  113.   if (!got_digit)
  114.     goto noconv;
  115.  
  116.   if (tolower (*s) == 'e')
  117.     {
  118.       /* Get the exponent specified after the `e' or `E'.  */
  119.       int save = errno;
  120.       char *end;
  121.       long int exp;
  122.  
  123.       errno = 0;
  124.       ++s;
  125.       exp = strtol (s, &end, 10);
  126.       if (errno == ERANGE)
  127.     {
  128.       /* The exponent overflowed a `long int'.  It is probably a safe
  129.          assumption that an exponent that cannot be represented by
  130.          a `long int' exceeds the limits of a `double'.  */
  131.       if (endptr != NULL)
  132.         *endptr = end;
  133.       if (exp < 0)
  134.         goto underflow;
  135.       else
  136.         goto overflow;
  137.     }
  138.       else if (end == s)
  139.     /* There was no exponent.  Reset END to point to
  140.        the 'e' or 'E', so *ENDPTR will be set there.  */
  141.     end = (char *) s - 1;
  142.       errno = save;
  143.       s = end;
  144.       exponent += exp;
  145.     }
  146.  
  147.   if (endptr != NULL)
  148.     *endptr = (char *) s;
  149.  
  150.   if (num == 0.0)
  151.     return 0.0;
  152.  
  153.   /* Multiply NUM by 10 to the EXPONENT power,
  154.      checking for overflow and underflow.  */
  155.  
  156.   if (exponent < 0)
  157.     {
  158.       if (num < DBL_MIN * pow (10.0, (double) -exponent))
  159.     goto underflow;
  160.     }
  161.   else if (exponent > 0)
  162.     {
  163.       if (num > DBL_MAX * pow (10.0, (double) -exponent))
  164.     goto overflow;
  165.     }
  166.  
  167.   num *= pow (10.0, (double) exponent);
  168.  
  169.   return num * sign;
  170.  
  171. overflow:
  172.   /* Return an overflow error.  */
  173.   errno = ERANGE;
  174.   return HUGE_VAL * sign;
  175.  
  176. underflow:
  177.   /* Return an underflow error.  */
  178.   if (endptr != NULL)
  179.     *endptr = (char *) nptr;
  180.   errno = ERANGE;
  181.   return 0.0;
  182.  
  183. noconv:
  184.   /* There was no number.  */
  185.   if (endptr != NULL)
  186.     *endptr = (char *) nptr;
  187.   return 0.0;
  188. }
  189.